<template>
  <div class="app-container">
    <form-header />
    <el-form ref="form" :model="form" :rules="rules" label-width="140px">
      <el-form-item label="图片">
        <el-upload
          action="#"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-change="handleChange"
          :auto-upload="false"
          :class="{hide:hideUpload}"
          :file-list="imgsList"
        >
          <i class="el-icon-plus" />
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" clearable />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <tinymce v-model="form.content" />
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <el-select v-model="form.categoryId" placeholder="请选择分类" class="filter-item">
          <el-option v-for="item in allClassifyList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch v-model="form.status" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { knowledgeDetail, knowledgeUpdate, knowledgeClassifyAllList } from '@/api/content'
import Tinymce from '@/components/Tinymce'
export default {
  name: 'KnowledgeEdit',
  components: { Tinymce },
  data() {
    return {
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' },
          { min: 0, max: 20, message: '标题不能超过20个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ],
        categoryId: [
          { required: true, message: '请选择分类', trigger: 'change' }
        ]
      },
      form: {
        id: '',
        title: '',
        topicPic: null,
        content: '',
        categoryId: '',
        status: 0
      },
      imgsList: [],
      hideUpload: false,
      imgFile: null,

      dialogImageUrl: '',
      dialogVisible: false,

      allClassifyList: []
    }
  },
  created() {
    this.form.id = this.$route.params['id']
    this.refresh()
  },
  mounted() {
    this.refreshClassify()
  },
  methods: {
    refreshClassify() {
      knowledgeClassifyAllList().then(response => {
        this.allClassifyList = response.data
      })
    },
    refresh() {
      knowledgeDetail({ id: this.form.id }).then(response => {
        const data = response.data
        this.form.title = data.title
        if (data.topicPic !== null && data.topicPic !== '') {
          this.imgsList = [
            { url: data.topicPic }
          ]
          this.hideUpload = true
        }
        this.form.topicPic = data.topicPic
        this.form.content = data.content
        this.form.categoryId = data.categoryId
        this.form.status = data.status === 1
      })
    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.imgFile === null && this.form.topicPic === null) {
            this.$message({ message: '请选择图片', type: 'error' })
            return
          }

          if (this.imgFile !== null) {
            this.uploadImgToOss(this.imgFile).then(response => {
              console.log('图片上传结果：' + JSON.stringify(response))
              this.form.topicPic = response
              this.goToSubmit()
            })
          } else {
            this.goToSubmit()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    goToSubmit() {
      this.form.status = this.form.status ? '1' : '0'
      knowledgeUpdate(this.form).then(response => {
        console.log(response)
        this.$message({ message: '提交成功', type: 'success' })
        this.$router.push({ name: 'KnowledgeList' })
      })
    },
    handleRemove(file, fileList) {
      console.log('handleChange')
      console.log(file)
      this.hideUpload = false
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleChange(file, fileList) {
      console.log('handleChange' + fileList.length)
      console.log(file)
      this.imgFile = fileList.length > 0 ? file : null
      this.hideUpload = fileList.length >= 1
    }
  }
}
</script>

<style lang="scss" scoped>
.hide /deep/.el-upload--picture-card {
	display: none;
}
</style>
